package com.jlastu.znb.view; // 替换为您的包名

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jlastu.znb.R; // 替换为您的R文件所在的包名
import com.jlastu.znb.contract.MessageContract; // 假设您有类似的接口定义
//import com.jlastu.znb.presenter.MessagePresenter; // 假设您有类似的Presenter类
import com.jlastu.znb.entity.ChatMessage;
import com.jlastu.znb.utils.SpUtils; // 如果您使用SpUtils，请确保导入正确的类

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment implements MessageContract.IMessageView {

    private TextView txt1;
    private TextView txt2;
    private TextView name1;
    private TextView name2;
    private static final String PREFS_NAME = "ChatPrefs";
    private static final String PREFS_KEY = "ChatMessages";

    private List<ChatMessage> chatMessages =new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载布局
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        txt1 = view.findViewById(R.id.txt1);
        txt2 = view.findViewById(R.id.txt2);
        name1 = view.findViewById(R.id.name1);
        name2 = view.findViewById(R.id.name2);

        txt1.setText(loadMessages("ChatMessages_"+name1.getText().toString()));
        txt2.setText(loadMessages("ChatMessages_"+name2.getText().toString()));
        view.findViewById(R.id.llMessage1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MessageChartActivity.class);
                intent.putExtra("user","150****8745");
                startActivityForResult(intent,100);
            }
        });
        view.findViewById(R.id.llMessage2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MessageChartActivity.class);
                intent.putExtra("user","小李飞刀");
                startActivityForResult(intent,100);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        txt1.setText(loadMessages("ChatMessages_"+name1.getText().toString()));
        txt2.setText(loadMessages("ChatMessages_"+name2.getText().toString()));
    }

    private String loadMessages(String currentUserKey) {
        SharedPreferences prefs = getActivity().getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String jsonString = prefs.getString(currentUserKey, "");
        if (!jsonString.isEmpty()) {
            try {
                JSONArray jsonArray = new JSONArray(jsonString);
                for (int i = 0; i < jsonArray.length(); i++) {
                    chatMessages.add(ChatMessage.fromJSON(jsonArray.getJSONObject(i)));
                }
                if (chatMessages.size()>0){
                    return chatMessages.get(chatMessages.size()-1).getMessage();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return "暂时没有新消息哦~";
            }
        }
        return "暂时没有新消息哦~";
    }

    @Override
    public void onResume() {
        super.onResume();
        // 这里可以添加当Fragment可见时需要执行的代码，比如刷新数据
        //mMessagePresenter.loadMessages(); // 假设您的Presenter有加载消息的方法
    }

    /*@Override
    public void showMessages(List<Message> messages) {
        // 展示消息数据
        // 这里需要您根据实际的消息数据类型和展示需求来编写代码
        // 例如，如果messages不为空，则更新UI展示消息，否则显示"暂时没有新消息哦~"
    }*/

    // 这里可以实现其他必要的方法，比如更新UI、处理用户交互等
}